/**
 * @author chenyz
 */
filefolder={
	readFile: function (myfile) {
		myfilereader = new FileReader();
		myfilereader.readAsText(myfile);
		myfilereader.onloadend = (function(theFile) {
	        return function(e) {
	        	var tabName = theFile.name;
	        	var tabContent = e.target.result;
	        	
	        	var editor = domtab.getContent(tabName);
	        	editor.value = tabContent;
	        	// bind editor to CodeMirror
	        	var myCodeMirror = CodeMirror.fromTextArea(editor, {lineNumbers: true,
	        		onChange:function(editor, changes) {
	        			var t = domtab.getTitle(domtab.activeFile);
	        			var status = t.getAttribute("status");
	        			if( status == "UNCHANGED") {
	        				t.setAttribute("status", "MODIFIED");
		        			t.innerHTML = "*" + t.innerHTML;
	        			}
	        		},
	    	        matchBrackets: true});
	        	myCodeMirror.setOption("mode", filefolder.getMIMEType(tabName));
	          };
	        })(myfile);

		myfilereader.onerror = (function(theFile) {
			// alert & remove the new generated tab
		})(myfile);
	},
	/* helper method */
	addEvent: function(elm, evType, fn, useCapture){
		if (elm.addEventListener) 
		{
			// for W3C browser
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			// for Internet Explorer
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			// for others
			elm['on' + evType] = fn;
		}
	},
	getMIMEType: function(fileTitle) {
		var type = fileTitle.split(".")[1];
		if(type == "js") {
			type = "text/javascript";
		} else if(type == "htm" || type == "html") {
			type = "text/html";
		} else if(type == "css") {
			type = "text/css";
		} else type = "text/plain";
		return type;
	}
};